package com.example.homework

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.fragment.app.Fragment

class GameFragment : Fragment(R.layout.fragment_game) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        view.findViewById<Button>(R.id.btn_start_snake).setOnClickListener {
            val intent = Intent(requireContext(), GameActivity::class.java).apply {
                // 把账号/用户名带过去
                putExtra("account", (requireActivity() as MainActivity).let {
                    com.example.homework.utils.Prefs.lastAccount(it) ?: ""
                })
                putExtra("username", (requireActivity() as MainActivity).let {
                    com.example.homework.db.UserDBHelper(it)
                        .getUserByAccount(
                            com.example.homework.utils.Prefs.lastAccount(it) ?: ""
                        )?.username ?: ""
                })
            }
            /* 使用 MainActivity 的 launcher 启动 */
            (requireActivity() as MainActivity).gameResultLauncher.launch(intent)
        }
    }
}